package com.lwk.jsonfiles;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.core.app.ActivityCompat;

import com.lwk.jsonfiles.Util.FileUtils;
import com.lwk.jsonfiles.Util.UtilDownload;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    public static final int GENERATE_CODE = 666;
    public static final int DECODE = 1;
    public static final int GENERATE = 2;
    private static String storePath = "/sdcard/Pictures/Json";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    /**
     * 申请权限.
     */
    private void requestPermission(int requestCode, int mode) {
        if (mode == DECODE) {
            decodePermission(requestCode);
        } else if (mode == GENERATE) {
            generatePermission(requestCode);
        }
    }

    /**
     * 申请权限.
     */
    private void decodePermission(int requestCode) {
        ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
                requestCode);
    }

    /**
     * 申请权限.
     */
    private void generatePermission(int requestCode) {
        ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                requestCode);
    }

    /**
     * 开始生成Json文件.
     */
    public void generateCodeBtnClick(View v) {
        requestPermission(GENERATE_CODE, GENERATE);

        File file=new File(storePath);
        File[] files=file.listFiles();
        if (files == null){
            Log.e("error","空目录");
        }
        List<String> s = new ArrayList<>();
        for(int i =0;i<files.length;i++){
            s.add(files[i].getName());
            String strname = files[i].getName();
            System.out.println("文件名："+strname);
            String strPath = files[i].getPath();
            System.out.println("文件路径："+strPath);
            String path = "/sdcard/Pictures/Json";
            FileUtils.isFileExist(strPath);
        System.out.println("下载文件内容："+UtilDownload.download(storePath));
        System.out.println("是否转移文件："+UtilDownload.downFile(storePath,strPath,strname));
        }
    }

}


